﻿@{
    ViewBag.Title = "选择合同";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<!--合同合同是属于项目的，此处是否应该先选择项目，再选择合同，且支持选择多合同-->
<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initProjectTable();  //项目列表
        initTypeTable();  //类别列表
        initContractTable();  //合同列表
    });
    //实现对DataGird控件的绑定操作
    //初始化项目列表
    function initProjectTable(queryData) {
        $('#projectDataGrid').datagrid({
            url: '/ProjectInfo/ProjectInfoList',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,
            toolbar: '#projectToolBar',
            columns: [[
                { title: '项目名称', field: 'ProjectName', width: 180, sortable: true },
            ]],
            onClickRow: function (index, row) {
                //var selectedRow = $("#projectDataGrid").datagrid("getSelected");
                //if (selectedRow) {
                //    $('#typeDataGrid').datagrid('load', { projectID: selectedRow.ID });
                //}

                var selectedRow = $("#projectDataGrid").datagrid("getSelected");
                if (selectedRow) {
                    //$('#contractDataGrid').datagrid('load', { bidID: selectedRow.ID, contractIDs: $("#tempContractIDs").val() });

                    var selectedTypeRow = $("#typeDataGrid").datagrid("getSelected");
                    if (selectedTypeRow) {
                        $('#contractDataGrid').datagrid('load', { pid: selectedRow.ID, typeName: selectedTypeRow.TypeName, ids: $("#tempContractIDs").val() });
                    }
                }
            }
        });
    }

    //初始化类别列表
    function initTypeTable(queryData) {
        $('#typeDataGrid').datagrid({
            url: '/ContractInfo/GetContractType',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            queryParams: queryData,
            toolbar: '#typeToolBar',
            columns: [[
                { title: '类别名称', field: 'TypeName', width: 180, sortable: true },
            ]],
            onClickRow: function (index, row) {
                var selectedRow = $("#typeDataGrid").datagrid("getSelected");
                if (selectedRow) {
                    //$('#contractDataGrid').datagrid('load', { bidID: selectedRow.ID, contractIDs: $("#tempContractIDs").val() });

                    var selectedProjectRow = $("#projectDataGrid").datagrid("getSelected");
                    if (selectedProjectRow) {
                        $('#contractDataGrid').datagrid('load', { pid: selectedProjectRow.ID, typeName: selectedRow.TypeName, ids: $("#tempContractIDs").val() });
                    }
                }
            }
        });
    }

    //初始化合同列表
    function initContractTable(queryData) {
        $('#contractDataGrid').datagrid({
            url: '/ContractInfo/GetTypeContractList',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#contractToolBar',
            columns: [[
                {
                    field: 'IsChecked',
                    formatter: function (value, row, index) {
                        if (value == true) {
                            return '<input type="checkbox" checked="true" onclick="ChooseContract.ChangeChecked(' + index + ');" />';
                        }
                        else {
                            return '<input type="checkbox" onclick="ChooseContract.ChangeChecked(' + index + ');" />';
                        }
                    }
                },
                { title: '合同名称', field: 'ContractName', width: 200, sortable: true },
            ]]
        });
    }

    var ChooseContract = {
        //刷新项目
        ReloadProject: function () {
            $("#projectDataGrid").datagrid("reload");
        },

        //刷新类别
        ReloadType: function () {
            $("#typeDataGrid").datagrid("reload");
        },

        //刷新合同
        ReloadContract: function () {
            $("#contractDataGrid").datagrid("reload");
        },

        //全选
        ChooseAll: function () {
            //var selectedRow = $("#projectDataGrid").datagrid("getSelected");  //获取选中行记录
            //if (selectedRow) {
            //    $.post("/Purview/ChooseAll?menuID=" + selectedRow.ID + "&isChecked=true");
            //    ChooseContract.ReloadType();
            //    ChooseContract.ReloadContract();
            //}

            //var dataRows = $("#projectDataGrid").datagrid("getRows");
            //if (dataRows != null && dataRows.length > 0) {
            //    for
            //}
            //var dataRows = $("#projectDataGrid").datagrid("getRows");
            //var i=0;
            //for(i=0;i<dataRows.length,i++){
            //    ChangeChecked(i);
            //}
            var dataRows = $("#contractDataGrid").datagrid("getRows");
            var i = 0;
            for (i = 0; i <= dataRows.length; i++) {
                //dataRows[i].IsChecked = true;
                ChooseContract.ChangeChecked(i);
            }
        },

        //全不选
        UnChooseAll: function () {
            var selectedRow = $("#projectDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                $.post("/Purview/ChooseAll?menuID=" + selectedRow.ID + "&isChecked=false");
                ChooseContract.ReloadType();
                ChooseContract.ReloadContract();
            }
        },

        //反选
        OppositeChoose: function () {
            var selectedRow = $("#projectDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                $.post("/Purview/OppositeChoose?menuID=" + selectedRow.ID);
                ChooseContract.ReloadType();
                ChooseContract.ReloadContract();
            }
        },

        //勾选或者取消勾选
        ChangeChecked: function (index) {
            //debugger;
            $("#contractDataGrid").datagrid("selectRow", index);  //选中指定行
            var selectedRow = $("#contractDataGrid").datagrid("getSelected");  //获取选中行记录
            if (selectedRow) {
                //var contractIDs = $("#contractIDs").val();  //取得当前的合同列表ID集合（格式：ID1,ID2,ID3）
                var tempContractIDs = $("#tempContractIDs").val();
                //$("#tempContractIDs").val(contractIDs);
                var contractIDSplist = [];
                if (tempContractIDs.length > 0) {
                    contractIDSplist = tempContractIDs.split(",");  //转为数组
                }

                //此处是为了取出IsChecked的值
                if (selectedRow.IsChecked == false) {
                    selectedRow.IsChecked = true;
                    //$.EasyuiSlide("不勾选");

                    //如果元素不存在，则添加到数组中
                    if (tempContractIDs.indexOf(selectedRow.ID) < 0) {
                        contractIDSplist.push(selectedRow.ID);
                    }
                }
                else {
                    selectedRow.IsChecked = false;
                    //$.EasyuiSlide("勾选");

                    //如果元素存在，则从数组中移除
                    if (tempContractIDs.indexOf(selectedRow.ID) >= 0) {
                        contractIDSplist = $.RemoveElement(contractIDSplist, selectedRow.ID);
                    }
                }
                //$.post("/Purview/ChangeChecked?menuActionID=" + selectedRow.ID + "&isChecked=" + selectedRow.IsChecked);  //xxxx由上面的方法获取
                $("#tempContractIDs").val(contractIDSplist.join(","));
            }
            //$.EasyuiSlide($("#tempContractIDs").val());  //测试用
            ////ChooseContract.ReloadContract();
        },
    }

</script>

<table>
    <tr>
        <!--左边为项目列表-->
        <td>
            <div region="west" data-options="border:false" style="width: 212px; height: 400px;">
                <div class="easyui-panel" title="项目列表">
                    <table id="projectDataGrid" style="height: 372px;"></table>
                    <div id="projectToolBar">
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseContract.ReloadProject()' data-options='plain:true,iconCls:"icon-application_lightning"'>刷新</a>
                    </div>
                </div>
            </div>
        </td>

        <!--中间为类别列表-->
        <td>
            <div region="center" style="border-left: 0px; width: 212px; height: 400px;">
                <div class="easyui-panel" title="类别列表">
                    <table id="typeDataGrid" style="height: 372px; "></table>
                    <div id="typeToolBar">
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseContract.ReloadType()' data-options='plain:true,iconCls:"icon-application_lightning"'>刷新</a>
                    </div>
                </div>
            </div>
        </td>

        <!--右边为合同列表-->
        <td>
            <div region="center" style="border-left: 0px; width: 265px; height: 400px;">
                <div class="easyui-panel" title="合同列表">
                    <table id="contractDataGrid" style="height: 372px; "></table>
                    <div id="contractToolBar">
                        @*<a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseContract.ChooseAll()' data-options='plain:true,iconCls:"icon-bestseller"'>全选</a>
                            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseContract.UnChooseAll()' data-options='plain:true,iconCls:"icon-blue_lego"'>全不选</a>
                            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseContract.OppositeChoose()' data-options='plain:true,iconCls:"icon-caution"'>反选</a>*@
                        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='ChooseContract.ReloadContract()' data-options='plain:true,iconCls:"icon-application_lightning"'>刷新</a>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</table>
